//////////////////////////////////////////////////////////////
// TITLE: 			Collapse Panels
// DESCRIPTION:
// Aggregate up from flows at security:mpid level to MastPerID:country:currency:class:subclass level, save panel_fundlevel_cp[m/q]
//////////////////////////////////////////////////////////////


use "$output/hd_panels/HD_all_panels_global_Q", clear
keep if inlist(DomicileCountryId, $eu1) | inlist(DomicileCountryId, $eu2) | inlist(DomicileCountryId, $eu3) | ///
	inlist(DomicileCountryId, "USA","GBR","CAN","CHE","AUS","SWE","DNK") | inlist(DomicileCountryId, "NOR","NZL")
drop date_m
format date %tq
collapse (sum) flow_usd* mvchange_constfx mvusd mvusd_l mvusd_constfx mvusd_constfx_l, by(mns_class mns_subclass MasterPortfolio Domicile date iso_country_code currency_id)  
merge m:1 MasterPortfolioId date using "$output/hd_panels/constant_panel_mpids_Q.dta"
keep if _merge==3 & constant_sample==1
drop constant_sample _merge
save "$output/hd_panels/panel_fundlevel_cp_global_Q.dta", replace
